<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // function foo() {
        //     return new Promise(function (resolve, reject) {
        //         setTimeout(function () {
        //             // resolve(123);
        //             reject('error')
        //         }, 1000);
        //     })
        // }
        // foo()
        //     .then(function (data) {
        //         console.log(data);
        //     })
        //     .catch(function (data) {
        //         console.log(data);
        //     })
        //     .finally(function () {
        //         console.log('finished');
        //     })

        function queryData(url) {
            return new Promise(function (resolve, reject) {
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState != 4) return;
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        resolve(xhr.responseText)
                    } else {
                        reject('出错了');
                    }
                }
                xhr.open('get', url)
                xhr.send(null)
            });
        }

        var p1 = queryData('http://localhost:3000/a1')
        var p2 = queryData('http://localhost:3000/a2')
        var p3 = queryData('http://localhost:3000/a3')
        // Promise.all([p1, p2, p3]).then(function (result) {
        //     console.log(result);
        // })
        Promise.race([p1, p2, p3]).then(function (result) {
            console.log(result);
        })
    </script>
</body>

</html>